import socket
a = 0
def receive_data_frame(data):
    global a
    frame_head = b'\x55\xAA\xFF\xFE'
    frame_tail = b'\xFE\xFF\xAA\x55'
    if(data[0:4] == frame_head and data[-4:] == frame_tail):
        print("frame_head",frame_head.hex())
        print("frame_tail", frame_tail.hex())
        type_id = data[4:5]
        print("type_id",type_id.hex())
        a += int(type_id.hex())
        data_length = data[5:7]
        print("data_length",data_length.hex())
        a += int(data_length.hex())
        check_data = data[-6:-4]
        print("check_data", check_data.hex())
        middle_data = data[7:-6] #middle_data是去掉数据包帧头振尾，类型ID数据包长度 校验位的数据
        return middle_data
    else:
        print("received error data frame")
def receive_middle_data(middle_data):
    global a
    cabinet_head =  b'\x55\xAA'
    cabinet_tail =  b'\xAA\x55'
    start = 0
    while True:
        start_index = middle_data.find(cabinet_head,start)  # Finding the start index
        if start_index == -1:
            break
        end_index = middle_data.find(cabinet_tail,start_index +len(cabinet_head))
        if end_index == -1:
            break
        middle_segment = middle_data[start_index:end_index + len(cabinet_tail)]
        receive_cabinet_data(middle_segment)
        start = end_index + len(cabinet_tail)
def receive_cabinet_data(middle_segment):
    global a
    cabinet_head =  b'\x55\xAA'
    cabinet_tail =  b'\xAA\x55'
    if(middle_segment[0:2] == cabinet_head and middle_segment[-2:] == cabinet_tail):
        print("cabinet_head", cabinet_head.hex())
        a += int(cabinet_head.hex(),16)
        print("cabinet_id",middle_segment[2:3].hex())
        a += int(middle_segment[2:3].hex())
        print("cabinet_num",middle_segment[3:4].hex())
        a += int(middle_segment[3:4].hex())
        box_head = b'\xFF\xFE'
        box_tail = b'\xFE\xFF'
        start = 0
        while True:
            start_index = middle_segment.find(box_head, start)  # Finding the start index
            if start_index == -1:
                break
            end_index = middle_segment.find(box_tail, start_index + len(box_head))
            if end_index == -1:
                break
            box_segment = middle_segment[start_index:end_index + len(box_tail)]
            receive_box_data(box_segment)
            start = end_index + len(box_tail)
        print("cabinet_tail", cabinet_tail.hex())
        a += int(cabinet_tail.hex(),16)
def receive_box_data(box_segment):
    global a
    box_head = b'\xFF\xFE'
    box_tail = b'\xFE\xFF'
    if (box_segment[0:2] == box_head and box_segment[-2:] == box_tail):
        print("box_head", box_head.hex())
        a += int(box_head.hex(),16)
        print("box_id",box_segment[2:3].hex())
        a += int(box_segment[2:3].hex())
        print("data_num", box_segment[3:4].hex())  #daiding
        a += int(box_segment[3:4].hex())
        data_num = int(box_segment[3:4].hex())
        data_index = 4
        for i in range(data_num):
            print("data_type",box_segment[data_index:data_index+1].hex())
            a += int(box_segment[data_index:data_index+1].hex())
            data_index = data_index+1
            print("data_name", box_segment[data_index:data_index+2].hex())
            a += int(box_segment[data_index:data_index+2].hex())
            data_index = data_index + 2
            print("time", int(box_segment[data_index:data_index+6].hex(),16))
            a += int(box_segment[data_index:data_index+6].hex(),16)
            data_index = data_index + 6
            print("data_quality",box_segment[data_index:data_index+1].hex())
            a += int(box_segment[data_index:data_index+1].hex())
            data_index = data_index + 1
            print("data", box_segment[data_index:data_index+1].hex())
            a += int(box_segment[data_index:data_index+1].hex())
            data_index = data_index + 4
        print("box_tail", box_tail.hex())
        a += int(box_tail.hex(),16)
def main():
    global a
    SERVER_IP = '192.168.0.121'
    SERVER_PORT = 12345
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind((SERVER_IP, SERVER_PORT))
    server_socket.listen(1)
    print("等待连接...")
    client_socket, client_address = server_socket.accept()
    data = client_socket.recv(1024)
    check_first = data[4:-6]
    sum_result = sum(int(byte) for byte in check_first)
    if hex(sum_result)[2:]== (data[-6:-4].hex()):
        print("效验通过")
        middle_data = receive_data_frame(data)
        receive_middle_data(middle_data)
        client_socket.close()
        server_socket.close()
    else:
        client_socket.close()
        server_socket.close()
if __name__ == "__main__":
    main()
